<script setup>
import {reactive,ref} from "vue";
import request from '@/api/request'
import {useRouter} from "vue-router"
import { Search } from '@element-plus/icons-vue'
let router = useRouter();
const rules = reactive({

})

let quotation=ref({})
let quotation_id=ref([])
let formRef=ref()

let isVisible=ref(false)
let title=ref("")
let data=ref({})
let User=ref([])

let warehouse=ref([])

let page=ref({
    current:1,
    size:10,
    total:0,
    records:[]
})

let toRemove=()=>{
    request({
        //查询客户
        url:"/quotation",
        method:"delete",
        data:quotation_id.value
    }).then(res=>{
        alert("删除成功")
        location();
    })
}
//修改
let toUpdate=async(row)=>{
    title.value="修改"
    isVisible.value=true;
    data.value=row;
    
    router.push("/sale/quotation/update");
}

let refTanle=()=>{
    isVisible.value=false;
    location();
}
//重置
let shuaxin=()=>{
    formRef.value.resetFields();
}
//新增
let toInsert=()=>{
    title.value="开单";
    isVisible.value=true;
    router.push("/sale/quotation/insert");
}

//选择框事件
let fuxuankuang=(a)=>{
    quotation_id.value = a
}
let isUpdate=(row)=>{
    if(row=="申请中"){
        return true;
    }
    return false;
}
let location=()=>{
    request({
        //查询员工
            url:"/system/user/list",
            method:"post",
            data:{
                current:1,
                size:1000
            }
        }).then(res=>{
            User.value=res.data.records
        })
    request({
        //查询客户
        url:"/quotation",
        method:"get",
        params:{
            current:page.value.current,
            size:page.value.size,
            ...quotation.value
    }
    }).then(res=>{
        page.value=res.data
    })
    request({
        //查询仓库
        url:"/warehouse",
        method:"get",
        params:{
            current:page.value.current,
            size:page.value.size
    }
    }).then(res=>{
        warehouse.value=res.data.date
    })


}

location();

//数据回收
let reclaim=()=>{
    title.value="数据回收";
    isVisible.value=true;
    router.push("/sale/quotation/reclaim");
}
</script>

<template>
    <div>
        <el-card>
            <template #header>
                <el-form ref="formRef" inline :model="quotation" style="display: flex;justify-content: start;" class="card_wrap">
                    <el-form-item label="报价单号" prop="quotationId" class="c_item">
                        <el-input v-model="quotation.quotationId" class="sousuo" placeholder="请输入单号"></el-input>
                    </el-form-item>
                    <el-form-item label="开单时间" prop="quotaitionDateOfSigning" class="c_item">
                        <el-date-picker v-model="quotation.quotaitionDateOfSigning" size="default" type="date" value-format="YYYY-MM-DD" placeholder="选择日期时间" @change="location">
                        </el-date-picker>
                        
                    </el-form-item>
                    <el-form-item label="客户名称" prop="customerResourcesName" class="c_item">
                        <el-input v-model="quotation.customerResourcesName" class="sousuo" placeholder="请输入名称"></el-input>
                    </el-form-item>
                    <el-form-item label="销售负责人" prop="quotationHeadOfSales" class="c_item">
                        <el-select class="sousuo" placeholder="销售负责人" v-model="quotation.quotationHeadOfSales" @change="location">
                        <el-option label="全部" value="全部">
                                </el-option>
                           <el-option v-for="item in User" :label="item.nickname" :value="item.id">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="出库仓库" prop="warehouseName" class="c_item">
                        <el-select class="sousuo" placeholder="仓库" v-model="quotation.warehouseId" @change="location">
                            <el-option label="全部" value="全部">
                                </el-option>
                            <el-option v-for="item in warehouse" :label="item.warehouseName" :value="item.warehouseId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="销售原价" prop="quotationSalesActually" class="c_item">
                        <el-input-number v-model="quotation.beginquotationSalesActually" style="width: 150px;" :min="0" :max ="1000000"></el-input-number>
                        ~
                        <el-input-number v-model="quotation.endquotationSalesActually" style="width: 150px;" :min="0" :max="1000000"></el-input-number>
                    </el-form-item>
                    
                    <el-form-item  class="c_item">
                        <template #default>
                             <el-button type="primary" @click="location">搜索</el-button>
                            <el-button type="info" :plain=true @click="shuaxin">重置</el-button><p></p>
                        </template>
                    </el-form-item>
                </el-form>

                <div style="margin: 20px 0px;">
                    <el-button @click="toInsert" type="primary" :plain="true">+添加</el-button>
                    <el-button type="danger" :plain=true @click="toRemove" style="border:0px" :disabled="!quotation_id.length">删除</el-button>

                </div>

                <el-table :data="page.records" :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }"  @selection-change="fuxuankuang" show-overflow-tooltip>
                    <el-table-column type="selection" width="55"/>
                    <el-table-column type="expand">
                        <template  #default="props">
                            <div m="4" style="margin: 0 20px;padding: 10px 0;" v-if="props.row.quoteDetails">
                                <el-table :data="props.row.quoteDetails" :border="false" style="border:0px" :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }" show-overflow-tooltip>
                                    <el-table-column prop="quoteDetailsId" label="明细码" width="90px;"/>
                                    <el-table-column prop="quoteDetailsQuotationId" label="报价单编码" width="100px;"/>
                                    <el-table-column prop="quoteDetailsProductsId" label="产品编码" width="80px;"/>
                                    <el-table-column prop="warehouse.warehouseName" label="仓库" width="80px;"/>
                                    <el-table-column prop="quoteDetailsNumberOfSales" label="销售数量" width="80px;"/>
                                    <el-table-column prop="quoteDetailsUnit" label="单位" width="60px;"/>
                                    <el-table-column prop="quoteDetailsUnitPriceOfTheSale" label="销售单价（含税）" width="140px;"/>
                                    <el-table-column prop="quoteDetailsVat" label="增值税税率" width="100px;"/>
                                    <el-table-column prop="quoteDetailsNoUnitPriceOfTheSale" label="销售单价（不含税）" width="150px;"/>
                                    <el-table-column prop="quoteDetailsPrice" label="销售原价（含税）" width="140px;"/>
                                    <el-table-column prop="quoteDetailsActualSellingPrice" label="实际售价/单价（含税）" width="180px;"/>
                                    <el-table-column prop="quoteDetailsActualTotal" label="实际合计价格（含税）" width="165px;"/>
                                    <el-table-column prop="quoteDetailsNoActualSellingPrice" label="实际售价/单价（不含税）" width="185px;"/>
                                    <el-table-column prop="quoteDetailsNoActualTotal" label="实际合计价格（不含税）" width="180px;"/>
                                    <el-table-column prop="quoteDetailsCost" label="成本单价" />
                                    <el-table-column prop="quoteDetailsTotalCost" label="成本合计" />
                                </el-table>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="quotationId" label="报价单编码" width="100px;"/>
                    <el-table-column prop="quotationTime" label="报价时间" width="120px;"/>
                    <el-table-column prop="customerResources.customerResourcesName" label="客户" width="100px;"/>
                    <el-table-column prop="nickname" label="销售负责人" width="100px;"/>
                    <el-table-column prop="quotationOriginalSalePrice" label="销售原价总额" width="110px;"/>
                    <el-table-column prop="quotationSalesActually" label="销售实际金额" width="110px;"/>
                    <el-table-column prop="quotationDiscountAmount" label="优惠金额" width="80px;"/>
                    <el-table-column prop="quotationDiscountRate" label="折扣率" width="80px;"/>
                    <el-table-column prop="quotationTheAmountIsCapitalized" label="金额大写" width="120px;"/>
                    <el-table-column prop="quotationTheAmountOfTheSale" label="销售金额（不含税）" width="150px;"/>
                    <el-table-column prop="quotaitionDateOfSigning" label="签订时间" width="110px;"/>
                    <el-table-column prop="nickname1" label="审批人" width="80px;"/>
                    <el-table-column prop="quotationState" label="状态" width="100px;"/>
                    <el-table-column prop="quotationProcurement" label="是否需要生产" width="110px;" #default="scope">
                        <el-tag :type="scope.row.quotationProcurement=='是' ? 'primary':'danger'">{{ scope.row.quotationProcurement}}</el-tag>
                    </el-table-column>
                    <el-table-column prop="quotaionShippingTime" label="发货时间" width="110px;"/>
                    <el-table-column prop="quotaionShippingAddress" label="发货地址" width="120px;"/>
                    <el-table-column prop="quotaionShippingFulladdress" label="详细地址" width="110px;"/>
                    <el-table-column label="操作" width="120px;" fixed="right">
                        <template #default="scope">
                            <el-button v-if="isUpdate(scope.row.quotationState)" type="primary" @click="toUpdate(scope.row)" :row="scope.row">修改</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-pagination @current-change="location" v-model:currentPage="page.current"
                    :page-sizes="[2, 4, 6, 8, 10]" :page-size="page.size" :total="page.total" background>
                </el-pagination>

                <el-dialog
                    :title="title"
                    v-model="isVisible"
                    :destroy-on-close="true"
                    width="60%">
                    <router-view @ref-table="refTanle" :data="data">
                    </router-view>
                </el-dialog>
            </template>
            
        </el-card>
       
    </div>
</template>

<style scoped>
.sousuo{
    width:200px;
    /* margin-right: 100px; */
}
.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.c_item{
    flex:1;
    margin:0px 5px 15px 0;
    width: calc((100%-10px)/3); 
    min-width:calc((100% - 10px)/3);
    max-width:calc((100% - 10px)/3);
    &:nth-child(3n){
        margin-right:0;
    }
}
[m='4'] :deep(.el-table){
    --el-table-border-color: transparent
}
:deep(.el-tooltip):hover{
    cursor: pointer;
}
</style>